package com.uxhao.orderservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uxhao.commonutils.entity.vo.CourseInfoCommonVo;
import com.uxhao.commonutils.entity.vo.UcenterMemberInfoCommonVo;
import com.uxhao.orderservice.client.EduClient;
import com.uxhao.orderservice.client.UcenterClient;
import com.uxhao.orderservice.constants.OrderStatus;
import com.uxhao.orderservice.constants.OrderPayType;
import com.uxhao.orderservice.entity.dashboard.DayDataVo;
import com.uxhao.orderservice.entity.dashboard.OrderAndAmountVo;
import com.uxhao.orderservice.entity.TOrder;
import com.uxhao.orderservice.entity.dashboard.WeekDataVo;
import com.uxhao.orderservice.mapper.TOrderMapper;
import com.uxhao.orderservice.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uxhao.orderservice.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author uxhao
 * @since 2022/01/22
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;


    @Override
    public String saveOrder(String courseId, String memberIdByJwtToken) {
        //远程调用课程服务，根据课程id获取课程信息
        CourseInfoCommonVo courseDto = eduClient.getCourseInfoDto(courseId);

        //远程调用用户服务，根据用户id获取用户信息
        UcenterMemberInfoCommonVo ucenterMember = ucenterClient.getInfo(memberIdByJwtToken);
        TOrder order = null;

        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", memberIdByJwtToken);
        wrapper.eq("status", OrderStatus.NOPAY.getCode());
        order = baseMapper.selectOne(wrapper);

        if (order == null) {
            order = new TOrder();
            //创建订单
            order.setOrderNo(OrderNoUtil.getOrderNo());
            order.setCourseId(courseId);
            order.setCourseTitle(courseDto.getTitle());
            order.setCourseCover(courseDto.getCover());
            order.setTeacherName(courseDto.getTeacherName());
            order.setTotalFee(courseDto.getPrice());
            order.setMemberId(memberIdByJwtToken);
            //TODO 手机号未写入订单
            //order.setMobile(ucenterMember.getMobile());
            order.setNickname(ucenterMember.getNickname());
            order.setStatus(OrderStatus.NOPAY.getCode());
            order.setPayType(OrderPayType.WX.getCode());
            baseMapper.insert(order);
        }

        return order.getOrderNo();
    }

    /**
     * @Description: 获取订单数量交易金额
     * @date 2022-04-04 17:21:55
     * @return:
     */
    @Override
    public OrderAndAmountVo countAllOrderAndAmount() {
        OrderAndAmountVo orderAndAmountVo = baseMapper.countAllOrderAndAmount();
        return orderAndAmountVo;
    }

    /**
     * @Description: 获取本周的订单和交易金额情况
     * @date 2022-04-04 17:22:18
     * @return:
     */
    @Override
    public WeekDataVo countCurrentOrderAndAmount() {
        List<DayDataVo> dayDataVos = baseMapper.countCurrentOrderAndAmount();
        List<Integer> days = new ArrayList<>();
        List<Integer> orders = new ArrayList<>();
        List<BigDecimal> amounts = new ArrayList<>();
        WeekDataVo weekDataVo = new WeekDataVo();
        for (DayDataVo dayDataVo : dayDataVos) {
            days.add(dayDataVo.getDay());
            orders.add(dayDataVo.getOrder());
            amounts.add(dayDataVo.getAmount());
        }
        weekDataVo.setDays(days);
        weekDataVo.setOrders(orders);
        weekDataVo.setAmounts(amounts);
        return weekDataVo;
    }

    /**
     * @Description: 获取上周的订单和交易金额情况
     * @date 2022-04-04 17:22:45
     * @return:
     */
    @Override
    public WeekDataVo countLastOrderAndAmount() {
        List<DayDataVo> dayDataVos = baseMapper.countLastOrderAndAmount();
        List<Integer> days = new ArrayList<>();
        List<Integer> orders = new ArrayList<>();
        List<BigDecimal> amounts = new ArrayList<>();
        WeekDataVo weekDataVo = new WeekDataVo();
        for (DayDataVo dayDataVo : dayDataVos) {
            days.add(dayDataVo.getDay());
            orders.add(dayDataVo.getOrder());
            amounts.add(dayDataVo.getAmount());
        }
        weekDataVo.setDays(days);
        weekDataVo.setOrders(orders);
        weekDataVo.setAmounts(amounts);
        return weekDataVo;
    }
}
